- /* timer.cpp by K.Tsuru */
- /**************************************************************
- Timer class
- reference:H.Hisano,C Magazine No.9(1993 Soft Bank,in Japanese)
- ***************************************************************/
- #include "timer.h"
-
- double Timer::unitSecond = -1.0;
- int Timer::timerObjects = 0;
- double Timer::lossTime[maxTimer];
-
- // constructor
- Timer::Timer(bool start):usesClock(true){ //Stand by in default.
- objectID = timerObjects; //It begins at 0.
- timerObjects++;
- if(unitSecond <= 0){
- int i;
- for(i=0; i<maxTimer;i++) lossTime[i] =0.0;
- GetUnitSecond();
- }
- active = start;
- if(active) Start();
- }
- //destructor
- Timer::~Timer(){
- lossTime[objectID] = 0.0;
- timerObjects--;
- }
-
- void Timer::GetUnitSecond(){
- unsigned n = 0;
- clock_t s = clock();
- clock_t e;
-
- while(clock() == s); /*wait next boundary*/
- e = clock();
- while(clock() == e) ++n; //wait within one step
- s = clock();
- unitSecond = (double(s - e)/CLOCKS_PER_SEC)/(double)n;
- }
-
- double Timer::WaitTick(double *lt){
- unsigned n = 0;
- double sec;
- clock_t s = clock(); //present value
-
- while(clock() == s) ++n; //wait next value
- s = clock(); //value after one step
- sec = (double)s/(double)CLOCKS_PER_SEC;//passed time since start of program
- //loss time in this routine
- *lt = (double)n*unitSecond;
- return sec;
- }
-
- double Timer::StopWatch(timer sw){
- double lt;
- int j;
- if(!active || (objectID >= maxTimer)) return 0;
- if(sw == START) {
- /*wait next tick and let it the starting time*/
- startClock = WaitTick(<);
- for(j = 0; j < objectID; j++) lossTime[j] += lt;
- startTime = time(NULL);
- return 0.0;
- }
-
// sw == STOP || LAP && active = true
- double t, dt;
- time_t now = time(NULL);
- dt = difftime(now, startTime);
- if(dt > 1000.0){
- usesClock=false;
- return dt;
- }
- /*wait next tick and let it the ending time*/
- double stop = WaitTick(<);
- for(j = 0; j < timerObjects; j++) lossTime[j] += lt;
- t = stop - startClock - lossTime[objectID];
- //It is possible t<0 by error. Order of -1.0e-5.
- if(t < 0.0) t = 0.0;
- return t;
- }
timer.cpp : last modifiled at 2017/04/06 15:01:06(2,141 bytes)
created at 2016/04/11 11:17:21
The creation time of this html file is 2017/10/07 10:54:16 (Sat Oct 07 10:54:16 2017).